
#ifndef SYSTEMCALL_H_
#define SYSTEMCALL_H_

#include "FileSystem.h"
#include "Inode.h"
#include "OpenFileDescTable.h"
#include <string>
#include <string.h>
#include <vector>


enum {R, RW}; // R = 0, RW = 1

class SystemCall{
public:
	SystemCall(LowLevelDisk* ll);
	virtual ~SystemCall();
	//	This layer keeps the open file table. This table is saved in memory (not on the disk):
	int MakeFile(char* file_name,char type, int access_permissions); // - creates and opens a new file named
			//file_name returning a fd to the new file. If type is soft-link mark it as such.
			//Opens the file with the specified permissions.
	int MakeHLink(char* target_file_name, char*file_name); // - creates a hard link with name file-name,
			//linked to target.
	int MakeDir(char* dir_name); // - creates a new directory named ‘dir-name’
	int RmDir(char* dir_name); // - removes the directory ‘dir-name’
	int RmFile(char* file_name); // - removes the file ‘file-name’
	int ls(char*dir_name, char * buf); // - lists the content of the location-directory. Return list in buf.
	int Open(char* filename, int /*flag*/ access_permissions); // - opens the file ‘file-name’. Needs to deal with soft-links. Opens the file with the specified permissions.
	int Close(int fd); // - closes the entry of the file in the file table, and frees the entry in the fd table.
	int Seek(int fd, int location); // - moves the file curser to ‘location’.
	int Read (int fd, int nBytes, char *Buffer); // - reads nBytes from the received file into the buffer.
	int Write (int fd, int nBytes,char * Buffer); // - writes nBytes from buffer into the file.
	int moveFile(char* parendDir, char * new_dest); // - Moves the child's i-node from its current directory
			//to new destination.
	bool isDir(char * address); // - Ret urns whether this address is a directory or not. Needs to deal
			//with soft-links.
//	int lockRead <fd>; // - locks file, so that only read operations are allowed. There is no limit
			//on the number of processes that are allowed to concurrently read the file. No processes are
			//allowed to write to the file if there is an open read lock on the file. It is not obligatory
			//to lock the file to read it. You should not block if it is locked rather return an error.
//	int lockWrite<fd> ; //-locks file, so that only one process may hold the lock. This process may
			//write to the file, and no other files may write to it or read from it. A child does not inherit
			//this lock. It is not obligatory to lock the file to write to it. You should not block if it is
			//locked rather return an error
//	int releaseLockRead<fd> ; //-releases the reading lock held by this process. Only the process which
			//locked the file may release the lock.
//	int releaseLockWrite<fd>; // -releases the writing lock held by this process. Only the process that
			//locked the file may release this lock.

private:

	FileSystem* _fileSystem;
//	vector<fileDesc> _fileDescTable;
	OpenFileDescTable* _descTable;
	vector<string> split(string s, char c);
	int GetFatherInode(char* path,bool create);
	int GetInodeByName(vector<LibEntry> vec, string name);
	string GetNameFromPath(char* path);
	void AddToDir(int inodeFather,string name, int newInode);
	/*Remember that read and write operations use the current file pointer to know where to start writing/reading from,
	and that these operation advances the file pointer.
	Allowing related processes to have the same fd table, and the same pointer into the file, demands implementing a
	more complex file descriptor table, as shown in class:*/

	/*Each process has its private descriptor table with 12 entries. Each open entry points to another table
	owned by the system-call layer which contains all open files. If the child inherited a fd from its
	parent it will point to the same entry as the parent. If it opened the file independently, it will
	point to a different entry.  This intermediate table consists of the offset to the current position
	inside the file, actions permitted on file (specified at open time) and i-node number. The maximum
	number of open files (including the same file which has been opened more than once) in the
	system cannot exceed 128.*/

	int GetInodeFromPath(char* path, bool create);
	LibEntry RmvFromDirByString(int inodeFather, string name);
	LibEntry RmvFromDirByInode(int inodeFather, int inode);

};

#endif /* SYSTEMCALL_H_ */
